package com.zx.tradeclient.ctrl.index4;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zx.base.ctrl.BaseActivity;
import com.zx.base.http.HttpUtils;
import com.zx.base.http.HttpUtils.CallBack;
import com.zx.base.util.ActivityTaskManager;
import com.zx.base.util.URLHelper;
import com.zx.tradeclient.R;
import com.zx.tradeclient.app.TradeApp;
import com.zx.tradeclient.ctrl.index3.CompanyInfoActivity;
import com.zx.tradeclient.ctrl.index5.LoginActivity;

public class ProductInfoActivity extends BaseActivity implements OnClickListener {
	/** Called when the activity is first created. */

	private String mProductID = "";

	private ImageView product_icon;
	private TextView product_info_price;
	private TextView product_info_summary;
	private TextView product_info_name;
	private RelativeLayout product_info_checkcompany;

	private ProgressDialog progressDialog;
	private TradeApp app;
	private String mCompanyID = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.product_info);

		if (!(ActivityTaskManager.getInstance().containsName("ProductInfoActivity"))) {
			ActivityTaskManager.getInstance().putActivity("ProductInfoActivity", this);
		}

		init();

	}

	private void init() {

		app = (TradeApp) getMyApplication();

		mProductID = getIntent().getStringExtra("productID");

		setHeader(0);
		header_right.setText("收藏");
		header_right.setVisibility(View.VISIBLE);
		header_right.setOnClickListener(this);

		product_icon = (ImageView) findViewById(R.id.product_info_icon);
		product_info_price = (TextView) findViewById(R.id.product_info_price);
		product_info_summary = (TextView) findViewById(R.id.product_info_summary);
		product_info_name = (TextView) findViewById(R.id.product_info_name);

		product_info_checkcompany = (RelativeLayout) findViewById(R.id.product_info_checkcompany);
		product_info_checkcompany.setOnClickListener(this);

		progressDialog = ProgressDialog.show(ProductInfoActivity.this, null, "正在获取数据，请稍后.....", true, true);

		getProductInfo();

	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == header_left) {
			goback();
		} else if (v == header_right) {

			if (app.user.getId().equals("")) {
				Intent intent = new Intent(ProductInfoActivity.this, LoginActivity.class);
				startActivity(intent);
			} else {
				progressDialog = ProgressDialog.show(ProductInfoActivity.this, null, "正在提交数据，请稍后.....", true, true);
				productFavorite();
			}
		} else if (v == product_info_checkcompany) {

			Intent intent = new Intent(ProductInfoActivity.this, CompanyInfoActivity.class);
			intent.putExtra("companyID", mCompanyID);
			startActivity(intent);

		}

	}

	private void getProductInfo() {

		Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
		map.put("act", "productDetail");
		map.put("id", mProductID);

		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				parseJson(response);

			}

			@Override
			public void onFailure(Throwable e) {
				progressDialog.dismiss();
				Toast toast2 = Toast.makeText(ProductInfoActivity.this, "错误", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();

			}
		});
	}

	private void parseJson(JSONObject response) {
		try {
			String succeed = response.getString("succeed");

			if (succeed.equals("000")) {

				app.imageLoader.DisplayImage(URLHelper.mPrefixImg + response.getString("photo"), product_icon, R.drawable.page_icon_default);

				product_info_name.setText(response.getString("name"));

				product_info_price.setText(response.getString("price"));

				product_info_summary.setText(response.getString("intro"));

				mCompanyID = response.getString("companyId");

			} else {
				Toast toast2 = Toast.makeText(ProductInfoActivity.this, "无数据", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();
			}

			progressDialog.dismiss();

		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	private void productFavorite() {

		Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
		map.put("act", "favourites");
		map.put("userId", app.user.getId());
		map.put("type", "2");
		map.put("infoId", mProductID);
		map.put("infoName", product_info_name.getText().toString());

		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				try {
					String succeed = response.getString("succeed");

					if (succeed.equals("000")) {

						Toast toast2 = Toast.makeText(ProductInfoActivity.this, "收藏成功", Toast.LENGTH_SHORT);
						toast2.setGravity(Gravity.CENTER, 0, 0);
						toast2.show();

					} else {
						Toast toast2 = Toast.makeText(ProductInfoActivity.this, "无数据", Toast.LENGTH_SHORT);
						toast2.setGravity(Gravity.CENTER, 0, 0);
						toast2.show();
					}

					progressDialog.dismiss();

				} catch (JSONException e) {
					e.printStackTrace();
				}

			}

			@Override
			public void onFailure(Throwable e) {
				progressDialog.dismiss();
				Toast toast2 = Toast.makeText(ProductInfoActivity.this, "错误", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();

			}
		});
	}

}